﻿package de.gameduell.framework.modules
{
	import de.gameduell.framework.application.controller.IController;
	import de.gameduell.framework.application.view.IView;
	import de.gameduell.framework.util.HashMap;
	import flash.events.EventDispatcher;
	
	import flash.events.Event;
	
	/**
	 * base module adpater for extension.
	 * 
 	 * @author ftru
 	**/
	public class ModuleAdapter extends EventDispatcher implements IModule {
		
		// injected on init, general usage
		protected var contextMap:HashMap;
		protected var view:IView;
		protected var controller:IController;
		
		public function ModuleAdapter(){
		}
		
		/**
		 * store context references for internal use and child creation.
		 **/		
		public function init(contextMap:HashMap):void {
			this.contextMap = contextMap;
			this.view = contextMap.getValue("view") as IView;
			this.controller = contextMap.getValue("controller") as IController;
		}
		
		/**
		 * allow to unregister event listeners and views before it will be removed
		 **/
		public function release():void {
		}
		
		/**
		 * an event occurs
		 **/
		public function handleEvent(command:Event = null):void {
		}		
		
		/**
		 * timer update
		 **/
		public function timerUpdate(subFrames:int, dt:Number):void {
		}		
		
		/**
		 * broadcast event
		 */
		public function broadcastEvent(e:Event):void {
			
			// dispatch over root node (no registration required)
			controller.dispatchEvent(e);
		
			// allow to register for events on this instance
			super.dispatchEvent(e);
		}
		
		/**
		 * dispatch event of classic flash model
		 **/
		public override function dispatchEvent (event:Event) : Boolean {
			
			// process internal
			this.handleEvent(event);
			
			// handle registered flash listeners
			return super.dispatchEvent(event);
		}		
				
		
	}
}